#include "CSwitchButton.h"

CSwitchButton::CSwitchButton(const QString& name, const QString& iPath,  const int& index ,const QString& cmdKey)
	:CSkipButton(name, iPath,index),_cmdKey(cmdKey),_textWidth(200)
{
}

void CSwitchButton::drawElement(QWidget* widget, QPainter* painter, bool bSelected, const QRect& rect)
{
	initTimer(widget);
	_isDrawFlag = true;
	_bSelected = bSelected;
	CSkipButton::setRect(rect);
	CSkipButton::drawIcon(painter);
	drawText(painter);
	drawStatus(painter);
	_isDrawFlag = false;
}

void CSwitchButton::drawStatus(QPainter* painter)
{
	if (!_bSelected)
		return;
	QPixmap pixmap(getCommandKeyIndex(_cmdKey)==1 ? ":/UI/icon/common/on.png" : ":/UI/icon/common/off.png");
	if (pixmap.isNull())
		return;
	QRect pixRect(_rect.topLeft()+QPoint(_pixRect.width()+8+_textWidth,34), QSize(58,30));
	painter->drawPixmap(pixRect, pixmap);

}

void CSwitchButton::drawText(QPainter* painter)
{
	if (!_bSelected)
		return;

	if (_drawLength > 0) {
		
		QFont font;
		font.setPixelSize(30);
		font.setFamily("Source Han Sans K");
		painter->setFont(font);
		QPen pen(QColor(255, 255, 255));
		painter->setPen(pen);
		QFontMetrics metrics(font);
		QString text(getTranslate(_name));

		int w = metrics.width(text);
		QRect textRect(_pixRect.topRight() + QPoint(8, 34), QSize(_textWidth, metrics.height()));
		if (w > textRect.width() && _timer != nullptr) {
			if (!_timer->isActive()) {

				_timer->start(_timerInterval);
			}
		}
		painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, text.right(_drawLength));
	}
}
